package com.jdjw.modules.bushouseapply.service.impl;

import com.jdjw.common.ResponseResult;
import com.jdjw.modules.bushouseapply.entity.BusHouseApply;
import com.jdjw.modules.bushouseapply.entity.BusHouseApplyVo;
import com.jdjw.modules.bushouseapply.mapper.BusHouseApplyMapper;
import com.jdjw.modules.bushouseapply.service.BusHouseApplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdjw.modules.sysdept.entity.SysDept;
import com.jdjw.modules.sysdept.mapper.SysDeptMapper;
import com.jdjw.modules.sysdept.service.SysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 困难户危旧房救助 服务实现类
 * </p>
 *
 * @author fq
 * @since 2021-07-22
 */
@Service
public class BusHouseApplyServiceImpl extends ServiceImpl<BusHouseApplyMapper, BusHouseApply> implements BusHouseApplyService {

    @Autowired
    private BusHouseApplyMapper busHouseApplyMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Override
    public ResponseResult queryAll(BusHouseApplyVo busHouseApplyVo) {
        ResponseResult rrt=new ResponseResult();
        try {
            //拿到数据起始位置
            int start=busHouseApplyVo.getPage()*busHouseApplyVo.getRow()-busHouseApplyVo.getRow();
            busHouseApplyVo.setPage(start);
            List<BusHouseApply> busHouseApplies =  busHouseApplyMapper.queryAll(busHouseApplyVo);
            for (BusHouseApply apply : busHouseApplies) {
                SysDept dept = sysDeptMapper.selectById(apply.getOrgCode());
                if(dept != null){
                    apply.setOrgCodeName(dept.getName());
                }
                    apply.setUserName(busHouseApplyVo.getUserName());
            }
            if("CJSBSQ".equals(busHouseApplyVo.getNodeLogo())){
                for (BusHouseApply apply : busHouseApplies) {
                    if("3".equals(apply.getStatus())){
                        apply.setStatus("6");
                    }
                }
            }
            rrt.setData(busHouseApplies);
            rrt.setCode(200);
            rrt.setMsg("查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            rrt.setData(null);
            rrt.setCode(500);
            rrt.setMsg("查询失败");
        }

        return rrt;
    }

    @Override
    public ResponseResult sqlEdit(String sqlEdit) {
        ResponseResult rrt=new ResponseResult();
        Boolean aBoolean = busHouseApplyMapper.sqlEdit(sqlEdit);
        if(aBoolean){
            rrt.setCode(200);
            rrt.setMsg("编辑成功");
        }else{
            rrt.setCode(200);
            rrt.setMsg("编辑失败");
        }
        return rrt;
    }
}
